package com.jinrustar.httpfileserver.util;

import org.apache.commons.net.util.Base64;

import java.awt.*;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;

public class CommonUtil {
    public static String formatDouble(double d) {
        DecimalFormat dec = new DecimalFormat(".00");

        return dec.format(d);
    }

    public static double formatDoubleWithROUND_HALF_UP(int newScale, double d) {
        BigDecimal b = new BigDecimal(d);
        return b.setScale(newScale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public static String formateDate(long t, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(t);
    }

    public static String formateDate(long t) {
        return formateDate(t, "yyyy/MM/dd HH:mm:ss");
    }

    public static byte[] encodeByBase64(byte[] bytes) {
        Base64 base64 = new Base64();
        return base64.encode(bytes);
    }

    public static String encodeToStringByBase64(byte[] bytes) {
        Base64 base64 = new Base64();
        return base64.encodeToString(bytes);
    }

    public static byte[] decodeByBase64(String s) {
        Base64 base64 = new Base64();
        return base64.decode(s);
    }

    public static void startDesktopBrowser(String url) {

        //判断是否支持Desktop扩展,如果支持则进行下一步
        if (Desktop.isDesktopSupported()) {
            try {
                URI uri = new URI(url);
                Desktop desktop = Desktop.getDesktop(); //创建desktop对象
                //调用默认浏览器打开指定URL
                desktop.browse(uri);

            } catch (URISyntaxException | IOException e) {
                e.printStackTrace();
            }
        }
    }


    public static void gotoUrlLinux(String url) {
        try {
            URI uri = new URI(url);
            java.awt.Desktop.getDesktop().browse(uri);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void gotoUrl(String url) {

        try {
            Runtime.getRuntime().exec("/usr/bin/firefox " + url);
        } catch (IOException e1) {
            e1.printStackTrace();
            startDesktopBrowser(url);
        }

//        try {
//            Runtime.getRuntime().exec("/usr/bin/chromium-browser " + url);
//        } catch (IOException e) {
//            e.printStackTrace();
//            try {
//                Runtime.getRuntime().exec("/usr/bin/firefox " + url);
//            } catch (IOException e1) {
//                e1.printStackTrace();
//                startDesktopBrowser(url);
//            }
//        }

        // Unix or Linux
//        String[] browsers = {"firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape", "chrome"};
//        String browser = null;
//        for (int count = 0; count < browsers.length && browser == null; count++)
//            // 执行代码，在brower有值后跳出，
//            // 这里是如果进程创建成功了，==0是表示正常结束。
//            if (Runtime.getRuntime().exec(new String[]{"which", browsers[count]}).waitFor() == 0)
//                browser = browsers[count];
//        if (browser == null)
//            throw new Exception("Could not find web browser");
//        else
//            // 这个值在上面已经成功的得到了一个进程。
//            Runtime.getRuntime().exec(new String[]{"xdg-open chrome", url});
    }


    public static void exec(String cmd) {

        try {
            Runtime.getRuntime().exec(cmd);
        } catch (IOException e) {
            e.printStackTrace();

        }
    }

    public static void notify(String title, String msg) {
        exec("notify-send " + title + " " + msg);
    }


}
